import { queryHotReview, updateHotReview, queryListByVolumeForHotReview } from '@/api/basicInfo'

const state = {
  goodsName: '',
  isDisabled: false,
  complete: false,
  greater: false,
  none: false,
  barCode: '',
  sn: '',
  planQuantity: '',
  form: {
    wavePickingCode: '',
    orderTag: '',
    shipmentName: '',
    carrierCode: '',
    carrierName: '',
    deliveryCode: '',
    shopNick: '',
    deliveryItemCode: '',
    outboundCode: ''
  },
  hideData: [],
  cb: () => {},
  rightData: [],
  leftData: [],
  modelPrintFlag: false,
  parcelType: '',
  printData: [],
  num: '',
  msg: '',
  numMsg: '',
  seleteData: [],
  cancelOrderPrompt: '' // 复核提示
}
const mutations = {
  SET_CANCEL_ORDER_PROMPT: (state, val) => {
    state.cancelOrderPrompt = val
  },
  SET_PACKINGDATA: (state, data) => {
    state.seleteData = data
  },
  SET_NUMMSG: (state, numMsg) => {
    state.numMsg = numMsg
  },
  SET_MSG: (state, msg) => {
    state.msg = msg
  },
  SET_PLANQUANTITY: (state, planQuantity) => {
    state.planQuantity = planQuantity
    state.num = planQuantity
  },
  SET_NUM: (state, num) => {
    state.num = num
  },
  SET_PRCELTYPE: (state, parcelType) => {
    state.parcelType = parcelType
  },
  SET_PRINTDATA: (state, printData) => {
    state.printData = printData
  },
  SET_FORM: (state, form) => {
    state.form = form
  },
  SET_GOODSNAME: (state, goodsName) => {
    state.goodsName = goodsName
  },
  SET_RIGHTDATA: (state, rightData) => {
    state.rightData = rightData
  },
  SET_LEFTDATA: (state, leftData) => {
    state.leftData = leftData
  },
  SET_ISDISABLED: (state, isDisabled) => {
    state.isDisabled = isDisabled
  },
  SET_CELLBACK: (state, cb) => {
    state.cb = cb
  },
  SET_COMPLETE: (state, complete) => {
    state.complete = complete
  },
  SET_NONE: (state, none) => {
    state.none = none
  },
  SET_GREATER: (state, greater) => {
    state.greater = greater
  },
  SET_SN: (state, sn) => {
    state.sn = sn
  },
  SET_BARCODE: (state, barCode) => {
    state.barCode = barCode
  },
  SET_HIDEDATA: (state, hideData) => {
    state.hideData = hideData
  },
  SET_FLAG: (state, modelPrintFlag) => {
    state.modelPrintFlag = modelPrintFlag
  }
}

const actions = {
  // 商品条码
  barCode({ commit }, val) {
    const { form } = state
    if (!form.wavePickingCode) return 1
    return true
  },
  query({ commit }, val) {
    const { wavePickingCode, goodsBarCode } = val
    const { cb, barCode } = state
    const params = { code: wavePickingCode, goodsBarCode }
    queryHotReview(params).then(res => {
      if ([400, 500].includes(res.status)) {
        return res
      }
      commit('SET_CANCEL_ORDER_PROMPT', res.data.outbound.cancelOrderPrompt)
      commit('SET_COMPLETE', false)
      commit('SET_GREATER', false)
      commit('SET_NONE', false)
      if (res.status !== 0 && res.msg) {
        commit('SET_NUMMSG', res.msg)
        commit('SET_GREATER', true)
        commit('SET_NONE', false)
        return false
      } else if (res.status !== 0 && !res.msg) {
        commit('SET_GREATER', false)
        commit('SET_NONE', false)
        commit('SET_NUMMSG', '')
        return false
      }
      if (res.data.status === 0) {
        const { deliverieList, goodsList, outbound } = res.data
        const params = { volume: deliverieList[0].volume, deliveryCode: deliverieList[0].code }
        queryListByVolumeForHotReview(params).then(req => {
          const daraArr = deliverieList.map(t => {
            return {
              ...t,
              packingMaterialList: req.data.packingMaterialList,
              packingMaterialName: req.data.packingMaterialList[0].name,
              packingMaterialCode: req.data.packingMaterialList[0].code
            }
          })
          commit('SET_PACKINGDATA', req.data.packingMaterialList)
          const form = {
            wavePickingCode: outbound.wavePickingCode,
            orderTag: translateStatus(res.data.outbound.orderTag),
            shipmentName: res.data.outbound.shipmentName,
            carrierCode: res.data.outbound.carrierCode,
            carrierName: res.data.outbound.carrierName,
            deliveryCode: '',
            code: res.data.deliverieList[0].code,
            shopNick: res.data.deliverieList[0].shopNick,
            deliveryItemCode: res.data.goodsList[0] && res.data.goodsList[0].code || '',
            outboundCode: res.data.outbound.code
          }
          if (barCode) {
            const obj = res.data.goodsList.find(v => barCode === v.goodsBarCode)
            if (!obj) {
              commit('SET_GOODSNAME', '')
              commit('SET_PLANQUANTITY', '')
            } else {
              const goodsName = obj.goodsName
              const planQuantity = res.data.outbound.itemPlanQuantity
              commit('SET_GOODSNAME', goodsName)
              commit('SET_PLANQUANTITY', planQuantity)
            }
          }
          commit('SET_FORM', form)
          // commit('SET_GOODSNAME', goodsList[0].goodsName)
          commit('SET_RIGHTDATA', daraArr)
          commit('SET_LEFTDATA', [outbound])
          commit('SET_ISDISABLED', true)
          commit('SET_HIDEDATA', goodsList)
          cb()
        })
      } else if (res.data.status === 1) {
        commit('SET_ISDISABLED', false)
        commit('SET_COMPLETE', true)
      } else if (res.data.status === 3) {
        commit('SET_NONE', true)
      }
    })
  },
  updateHotReviewFun({ commit }, vals) {
    const { val } = vals
    const { barCode, form, sn, cb } = state
    // const obj = hideData.find(v => barCode === v.goodsBarCode)
    const { wavePickingCode, outboundCode } = form
    const parms = {
      code: wavePickingCode,
      // deliveryCode: obj.deliveryCode,
      goodsBarCode: barCode,
      reviewCount: Number(val),
      // deliveryItemCode: obj.code,
      outboundCode: outboundCode,
      action: sn,
      packingMaterialCode: state.rightData[0].packingMaterialCode
    }
    return updateHotReview(parms).then(res => {
      if ([400, 500].includes(res.status)) {
        return res
      }
      commit('SET_CANCEL_ORDER_PROMPT', res.data.outbound.cancelOrderPrompt)
      commit('SET_GREATER', false)
      commit('SET_NONE', false)
      if (res.status !== 0 && res.msg) {
        commit('SET_NUMMSG', res.msg)
        commit('SET_GREATER', true)
        commit('SET_NONE', false)
        return false
      } else if (res.status !== 0 && !res.msg) {
        commit('SET_GREATER', false)
        commit('SET_NONE', false)
        commit('SET_NUMMSG', '')
        return false
      }
      // if (res.data.status === 2) {
      //   commit('SET_GREATER', true)
      //   commit('SET_NONE', false)
      //   return false
      // } else if (res.data.status === 3) {
      //   commit('SET_NONE', true)
      //   commit('SET_GREATER', false)
      //   return false
      // }

      const obj = res.data.goodsList.find(v => barCode === v.goodsBarCode)
      if (!obj) return false
      const goodsName = obj.goodsName
      const planQuantity = res.data.outbound.itemPlanQuantity
      commit('SET_GOODSNAME', goodsName)
      commit('SET_PLANQUANTITY', planQuantity)
      const { seleteData } = state
      if (res.data.status === 0) {
        const form = {
          wavePickingCode: res.data.outbound.wavePickingCode,
          orderTag: translateStatus(res.data.outbound.orderTag),
          shipmentName: res.data.outbound.shipmentName,
          carrierCode: res.data.outbound.carrierCode,
          carrierName: res.data.outbound.carrierName,
          deliveryCode: obj.deliveryCode,
          shopNick: res.data.deliverieList[0].shopNick,
          deliveryItemCode: res.data.goodsList[0] && res.data.goodsList[0].code || '',
          outboundCode: res.data.outbound.code,
          code: res.data.deliverieList[0].code
        }
        const arr = [res.data.outbound]
        commit('SET_HIDEDATA', res.data.goodsList)
        commit('SET_RIGHTDATA', res.data.deliverieList.map(v => {
          return { ...v, packingMaterialList: seleteData, packingMaterialName: seleteData[0].name, packingMaterialCode: seleteData[0].code }
        }))
        commit('SET_FORM', form)
        commit('SET_LEFTDATA', arr)
        if (res.data.printMap && JSON.stringify(res.data.printMap) !== '{}') {
          const obj = {
            wavePickingCode: res.data.outbound.wavePickingCode,
            orderTag: translateStatus(res.data.outbound.orderTag),
            shipmentName: res.data.outbound.shipmentName,
            carrierCode: res.data.outbound.carrierCode,
            carrierName: res.data.outbound.carrierName,
            deliveryCode: '',
            shopNick: res.data.deliverieList[0].shopNick,
            deliveryItemCode: '',
            outboundCode: res.data.outbound.code,
            code: res.data.deliverieList[0].code
          }
          const arr = [res.data.outbound]
          commit('SET_LEFTDATA', arr)
          commit('SET_FORM', obj)
          commit('SET_ISDISABLED', false)
          if (res.data.printMap.status === '0') {
            const str = res.data.printMap.logisticsCode
            if (str) {
              commit('SET_PRCELTYPE', str)
              commit('SET_PRINTDATA', res.data.printMap.data)
              commit('SET_FLAG', true)
              return 1
            }
            return 7
          } else {
            commit('SET_MSG', res.data.printMap.message)
            return 9
          }
        }
        cb()
        return true
      } else if (res.data.status === 1 && res.data.printMap && JSON.stringify(res.data.printMap) !== '{}') {
        commit('SET_COMPLETE', true)
        commit('SET_GREATER', false)
        commit('SET_NONE', false)
        // if () {
        const obj = {
          wavePickingCode: res.data.outbound.wavePickingCode,
          orderTag: translateStatus(res.data.outbound.orderTag),
          shipmentName: res.data.outbound.shipmentName,
          carrierCode: res.data.outbound.carrierCode,
          carrierName: res.data.outbound.carrierName,
          deliveryCode: '',
          code: res.data.deliverieList[0].code,
          shopNick: res.data.deliverieList[0].shopNick,
          deliveryItemCode: '',
          outboundCode: res.data.outbound.code
        }
        const arr = [res.data.outbound]
        commit('SET_LEFTDATA', arr)
        commit('SET_FORM', obj)
        commit('SET_HIDEDATA', res.data.goodsList)
        commit('SET_RIGHTDATA', res.data.deliverieList.map(v => {
          return { ...v, packingMaterialList: seleteData, packingMaterialName: seleteData[0].name, packingMaterialCode: seleteData[0].code }
        }))
        commit('SET_ISDISABLED', false)
        if (res.data.printMap.status === '0') {
          const str = res.data.printMap.data[0].logisticsCode
          if (str) {
            commit('SET_PRCELTYPE', str)
            commit('SET_PRINTDATA', res.data.printMap.data)
            commit('SET_FLAG', true)
            return 1
          }
          return 7
        } else {
          commit('SET_MSG', res.data.printMap.message)
          return 9
        }
      }
    })
  },
  resets({ commit }) {
    commit('SET_FORM', {})
    commit('SET_GOODSNAME', '')
    commit('SET_RIGHTDATA', [])
    commit('SET_LEFTDATA', [])
    commit('SET_COMPLETE', false)
    commit('SET_GREATER', false)
    commit('SET_NONE', false)
    commit('SET_PRINTDATA', [])
    commit('SET_PRCELTYPE', '')
    commit('SET_ISDISABLED', false)
    commit('SET_FLAG', false)
    commit('SET_NUM', '')
    commit('SET_GOODSNAME', '')
    commit('SET_PLANQUANTITY', '')
  }
}
function translateStatus(val) {
  if (val === '1') return '单品单件'
  if (val === '2') return '单品多件'
  if (val === '3') return '多品'
  if (val === '4') return '爆品'
  if (val === '5') return '按单'
}
export default {
  namespaced: true,
  state,
  mutations,
  actions
}
